<include file="Public/header"/>
<style>
    .park-form__radioInput.cur{background:url(__ROOT__/Public/Home/images/radio_press.png) no-repeat;background-size:20px;}
    .price-update-input .input{border:1px solid #e0e0e0;padding:5px;margin-top:10px;border-radius:5px;}
</style>
<body class="body-bg">
<div class="park-header">
    <div class="park-header__title">商品管理</div>
    <div class="park-header__left" onclick="window.location='/Business/User/goods/';">
        <i class="iconfont icon-fanhui"></i>
    </div>
    <div class="park-header__right" onclick="window.location='/Business/User/goods_shelves/cat_id/{$cat_id}';">
        <span>添加</span>
    </div>
</div>
<div class="container mb-goodsManage">
    <div class="park-goods-category">
        <div id="category-title" class="park-goods-category__title">
            <span>{$cate_list[0].name}</span>
            &nbsp;&nbsp;
            <i class="iconfont icon-xia"></i>
        </div>

        <div id="category-list" class="park-goods-category__list">
            <ul>
                <volist name="cate_list" id="val">
                    <li <if condition="$val['id'] eq $sid">class="active"</if> onclick="window.location = '/Business/User/goods_list/cat_id/{$cat_id}/sid/{$val.id}';">{$val.name}</li>
                </volist>
            </ul>
        </div>
    </div>
    <div class="park__goods">
        <ul id="goods_list"></ul>
        <div style="text-align:center;background:#fff;padding:30px 0;display:none;" id="no_goods">
            <img style="" width="30%" src="__ROOT__/Public/Home/images/no-goods.png">
            <div style="margin-top:15px;color:#ccc;">暂无更多商品</div>
        </div>
    </div>
</div>
<div class="park-goodsManage-footer">
    <div class="park-goodsManage-edit">
        <div class="park-goodsManage-edit__radio" onclick="all_check(this);">
            <label class="park-form">
                <i class="park-form__radioInput park-form__radio"></i>
                <span class="park-form__text">全选</span>
            </label>
        </div>
        <div class="park-goodsManage-edit__sellOut" onclick="sell_out(1);">售罄</div>
        <div class="park-goodsManage-edit__sellNot" onclick="sell_out(2)">下架</div>
    </div>
</div>
<div class="park-mask" style="z-index: 98"></div>
<script src="__ROOT__/Public/Home/js/All.js"></script>
<script src="__ROOT__/Public/Home/js/PriceUpdate.js"></script>
<script src="__ROOT__/Public/Home/layer/layer.js"></script>
<script>
    var options = {
        type: 'get', // default 'get'
        dataType: 'html' // default 'json'
    };
    $(function () {
        var url = '/Business/User/ajaxGoodsList/';
        var sid = '{$sid}';
        var params = {'sid':sid};
        PARK.dropLoad(PARK.dropAjax(options, url, params, function (data) {
            var data = JSON.parse(data);
            var html = '';
            for (var i = 0; i < data.result.length; i++) {
                var cur = '';
                html+='<li>';
                html+='    <div class="park-goods__radio">';
                html+='        <label class="park-form">';
                html+='            <i class="park-form__radioInput park-form__radio" data-id="'+data['result'][i]['spec_id']+'" onclick="goods_check(this);"></i>';
                html+='        </label>';
                html+='    </div>';
                if(data['result'][i]['is_sold']==1){
                    cur = 'saleout';
                }
                html+='    <div class="park-goods__pic '+cur+'" style="background-image: url(' + data.result[i].thumb + ')"></div>';
                html+='    <div class="park-goods__info">';
                html+='        <p class="name">'+ data['result'][i]['goods_name'] +'</p>';
                html+='        <p class="spec">规格:' + data['result'][i]['spec_name'] + '</p>';
                html+='        <p class="price-line">';
                html+='            <span class="price">￥<em>' + data['result'][i]['left_price'] + '</em>' + data['result'][i]['right_price'] + '</span>';
				if(data['result'][i]['spec_mk_price']){
					html+='            <span class="Oprice">￥<em>'+data['result'][i]['spec_mk_price']+'</em></span>';
				}
                html+='        </p>';
                html+='    </div>';
                html+='   <div class="park-goods__pos">';
                html+='        <div class="park-goods__editPrice" data-original="'+data['result'][i]['shop_price']+'" data-special="" data-id="'+data['result'][i]['spec_id']+'">编辑价格</div>';
                html+='    </div>';
                html+='</li>';
            }
            if(html){
                $('#goods_list').append(html);
            }else{
                $('#no_goods').show();
            }
            if(data.is_page==1){
                PARK.unDropLoad();
            }
        }));

        // 全选
        new All($('#all'), $('.cRadio'));

        var category_title = {
            showList: function () {
                $('#category-list').show();
            },
            hideList: function () {
                $('#category-list').hide();
            },
            toggleList: function () {
                $('#category-list').toggle();
            },
            initCategory: function (li) {
                $('#category-list li').removeClass('active');
                $(li).addClass('active');
            },
            setTitle: function (title) {
                $('#category-title').find('span').text(title);
            },
            bindEvent: function () {
                var _this = this;
                $(document).on('click', '#category-title', function (ev) {
                    _this.toggleList();
                    PARK.toggleMask();
                });
                $(document).on('click', '#category-list li', function (ev) {
                    _this.hideList();
                    PARK.hideMask();
                    _this.initCategory(this);
                    _this.setTitle($(this).text());
                });
            },
            init: function () {
                this.bindEvent();
            }
        };

        category_title.init();

        $(document).on('click', '.park-mask', function (ev) {
            category_title.hideList();
        });

        // 修改价格
        var model = new PriceUpdate('', 'get');

        $(document).on('click', '.park-goods__editPrice', function () {
            model.showModel($(this).attr('data-original'), $(this).attr('data-special'), $(this).attr('data-id'));
        });
    });
    function sub_price(){
        var original_price = $('#original_price').val();
        var specialInput = $('#specialInput').val();
        if($("#isSpecial").is(':checked') && specialInput>0){
            if(specialInput>original_price){
                layer.open({
                    content:'特价金额不能大于正价金额'
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
                return false;
            }
        }
        var spec_id = $('#spec_id').val();
        $.post('/Business/User/check_goods/',{'spec_id':spec_id,'spec_price':original_price,'spec_mk_price':specialInput},function(data){
            if(data.code==200){
                layer.open({
                    content:data.msg
                    ,btn: ['确定']
                    ,yes: function(index){
                        window.location = '/Business/User/goods_list/cat_id/{$cat_id}/sid/{$sid}';
                    }
                });
            }else{
                layer.open({
                    content:data.msg
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
            }
        },'json');
        $('.price-update').remove();
        return false;
    }
    function goods_check(obj){
        if($(obj).hasClass('cur')){
            $(obj).removeClass('cur');
        }else {
            $(obj).addClass('cur');
        }
    }
    function all_check(obj){
        $('.park-form__radioInput').each(function(){
            if($(obj).find('.park-form__radio').hasClass('cur')){
                $(this).removeClass('cur');
            }else{
                $(this).addClass('cur');
            }
        });
    }
    //设为售罄
    function sell_out(num){
        window.all_id = '';
        $('.park-form__radioInput.cur').each(function(){
            var id = $(this).attr('data-id');
            if(id) {
                window.all_id += id + ',';
            }
        });
        if(!all_id){
            layer.open({
                content:'请先选中商品'
                ,skin: 'msg'
                ,time: 2 //2秒后自动关闭
            });
            return false;
        }
        $.post('/Business/User/sell_out/',{'spec_id':all_id,'num':num},function(data){
            if(data.code==200){
                layer.open({
                    content:data.msg
                    ,btn: ['确定']
                    ,shadeClose:false
                    ,yes: function(index){
                        window.location = '/Business/User/goods_list/cat_id/{$cat_id}/sid/{$sid}';
                    }
                });
            }else{
                layer.open({
                    content:data.msg
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
            }
        },'json');
    }
</script>
</body>
</html>